package com.darwin.darwinUI.general
{	
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * 字体
	 * @author Roland
	 */
	public class DarwinFont 
	{
		/**
		 * 默认字体 
		 */
		public static var DEFAULTFONT:String = "Tahoma";
		
//		/**
//		 * 属性没设为 LANG_EMBED 时判断的语言列表
//		 */
//		public static var INCLUDE_LANG_EMBED:String = "en";
		
		/**
		 * 使用嵌入 
		 */
		public static const USE_EMBED:String = "useEmbed";
		
		/**
		 * 不使用嵌入 
		 */
		public static const NONUSE_EMBED:String = "nonuseEmbed";
		
//		/**
//		 * 根据语言来嵌入, 如果当前的语言在 INCLUDE_LANG_EMBED属性设定则嵌入,否则不嵌入
//		 */
//		public static const LANG_EMBED:String = "langEmbed";
		
		
		private var name:String;
		private var size:uint;
		private var bold:Boolean;
		private var italic:Boolean;
		private var underline:Boolean;
		private var textFormat:TextFormat;
		private var textAlign:String;
		private var leading:int;
		private var _color:uint = 0x000000;
		private var letterSpacing:Object;
		
		private var advancedProperties:FontAdvProperties;
		
		/**
		 * 
		 * @param	name	字体名
		 * @param	size	字体大小
		 * @param	bold	粗体
		 * @param	italic	斜体
		 * @param	underline	下划线
		 * @param	embedFontsOrAdvancedPros	是否是嵌入字体(值为DarwinFont.LANG_EMBED,DarwinFont.NONUSE_EMBED,DarwinFont.USE_EMBED),
		 * 										或者使用FontAdvProperties类对字体进行一些高级设置
		 * @param 	align 对齐方式
		 * @param 	leading 行距
		 * @param letterSpacing 字符间距(默认为null，设置为Number型数据开启)
		 */
		public function DarwinFont(name:String = null, size:uint = 11, bold:Boolean = false, italic:Boolean = false, underline:Boolean = false, embedFontsOrAdvancedPros:*= null, align:String="left", leading:int = 0,letterSpacing:Object = null)
		{
			if(name==null)	{
				name = DEFAULTFONT;
			}
			
			this.name = name;
			this.size = size;
			
			this.bold = bold;
			this.italic = italic;
			this.underline = underline;
			if (embedFontsOrAdvancedPros is FontAdvProperties)
			{
				advancedProperties = embedFontsOrAdvancedPros as FontAdvProperties;
			}else 
			{
				advancedProperties = new FontAdvProperties(embedFontsOrAdvancedPros);
			}
			this.textAlign = align;
			this.leading = leading;
			this.letterSpacing = letterSpacing;
			textFormat = getTextFormat();
		}
		
		/**
		 * 获取字体名
		 * @return	
		 */
		public function getName():String{
			return name;
		}
		
		/**
		 * 不会修改原来对象的值，返回一个新的DarwinFont对象
		 * @param	name	新的字体名
		 * @return
		 */
		public function changeName(name:String):DarwinFont{
			return new DarwinFont(name, size, bold, italic, underline, advancedProperties,textAlign,leading,letterSpacing);
		}
		
		/**
		 * 获取字体大小
		 * @return
		 */
		public function getSize():uint{
			return size;
		}
		
		/**
		 * 不会修改原来对象的值，返回一个新的DarwinFont对象
		 * @param	size	新的字体大小
		 * @return
		 */
		public function changeSize(size:int):DarwinFont{
			return new DarwinFont(name, size, bold, italic, underline, advancedProperties,textAlign,leading,letterSpacing);
		}
		
		/**
		 * 获取字体是否是粗体
		 * @return
		 */
		public function isBold():Boolean{
			return bold;
		}
		
		/**
		 * 不会修改原来对象的值，返回一个新的DarwinFont对象
		 * @param	bold
		 * @return
		 */
		public function changeBold(bold:Boolean):DarwinFont{
			return new DarwinFont(name, size, bold, italic, underline, advancedProperties,textAlign,leading,letterSpacing);
		}
		
		/**
		 * 获取字体是否是斜体
		 * @return
		 */
		public function isItalic():Boolean{
			return italic;
		}
		
		/**
		 * 不会修改原来对象的值，返回一个新的DarwinFont对象
		 * @param	italic
		 * @return
		 */
		public function changeItalic(italic:Boolean):DarwinFont{
			return new DarwinFont(name, size, bold, italic, underline, advancedProperties,textAlign,leading,letterSpacing);
		}
		
		/**
		 * 获取字体是否有下划线
		 * @return
		 */
		public function isUnderline():Boolean{
			return underline;
		}
		
		/**
		 * 不会修改原来对象的值，返回一个新的DarwinFont对象
		 * @param	underline
		 * @return
		 */
		public function changeUnderline(underline:Boolean):DarwinFont{
			return new DarwinFont(name, size, bold, italic, underline, advancedProperties,textAlign,leading,letterSpacing);
		}
		
		/**
		 * 字体是否绑定
		 * @return
		 */
		public function isEmbedFonts():Boolean{
			return advancedProperties.isEmbedFonts();
		}
		
		/**
		 * 返回字体高级设置的属性对象
		 * @return
		 */
		public function getAdvancedProperties():FontAdvProperties{
			return advancedProperties;
		}
		
		/**
		 * 应用字体
		 * @param	textField
		 * @param	beginIndex
		 * @param	endIndex
		 */
		public function apply(textField:TextField, beginIndex:int = -1, endIndex:int = -1):void	{
			advancedProperties.apply(textField);
			textField.setTextFormat(textFormat, beginIndex, endIndex);
			textField.defaultTextFormat = textFormat;
		}
		
		/**
		 * 获取AS原生的TextFormat对象
		 * @return
		 */
		public function getTextFormat():TextFormat{
			var textFormat:TextFormat = new TextFormat(
				name, size, null, bold, italic, underline, 
				"", "", textAlign, 0, 0, 0, leading 
			);
			if(letterSpacing!=null)
			{
				textFormat.kerning = true;
				textFormat.letterSpacing = letterSpacing;
			}else{
				textFormat.kerning = false;
				textFormat.letterSpacing = null;
			}
			return textFormat;
		}
		
		public function clone():DarwinFont	{
			return new DarwinFont(name, size, bold, italic, underline, advancedProperties,textAlign,leading,letterSpacing);
		}
		
		public function toString():String	{
			return "DarwinFont[" 
			+ "name : " + name 
				+ ", size : " + size 
				+ ", bold : " + bold 
				+ ", italic : " + italic 
				+ ", underline : " + underline 
				+ ", advanced : " + advancedProperties 
				+ ", align : " + textAlign 
				+ ", leading : " + leading 
				+ ", letterSpacing : " + letterSpacing 
				+ "]";
		}

		public function get color():uint
		{
			return _color;
		}

		public function set color(value:uint):void
		{
			_color = value;
		}

	}
}